<?php
class Popup_Model_Popup extends Zend_Db_Table{
	
	protected $_name = 'popup';
	protected $_primary ='id';
	
	protected $_ids;
	
	public function getItem($arrParam = null, $options = null){
		
		if($options['task'] == 'admin-info' || $options['task'] == 'admin-edit'){
			 
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter, $config);
			$select = $db->select()
							->from('popup AS po',array('*',"DATE_FORMAT(po.create,'%d/%m/%Y') AS create"))
							->joinLeft('users AS u','po.id_user=u.id',array('u.name AS name_user'))
							->where('po.id = ?',$arrParam['id'],INTERGER);
			
			$result = $db->fetchRow($select);
		}
		
		if($options['task'] == 'public-detail'){
			$select = $this->select()
					->where('id = ?',$arrParam['id'],INTERGER);
			$result = $this->fetchRow($select)->toArray();
		}
		
		if($options['task'] == 'public-category'){
			if(!empty($arrParam['cid'])){
				$db = Zend_Registry::get('connectDb');
				//$db = Zend_Db::factory($adapter, $config);
				$select = $db->select()
				->from('clb AS cc')
				->where('cc.id = ?', $arrParam['cid'], INTEGER);
				$result = $db->fetchRow($select);
			}else{
				$language = Zend_Registry::get('language');
				$result = array('name' => $language['language']['baiViet'],
						'meta_title' => '',
						'meta_description' => '',
						'meta_keywords' => ''
				);
			}
		}
		
		return $result;
	}
	
	public function ClubInSelectbox($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		$ssFilter  = $arrParam['ssFilter'];
		if($options == null){
			$select = $db->select()
			->from('clb AS cc',array('id','name','status'))
			->order('cc.id ASC');
			$result = $db->fetchPairs($select);
	
		} 
		return $result;
	}
	
	
	
	public function getUser($arrParam = null, $options = null){
	
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		//var_dump($arrParam);
		$id_kh=$arrParam['id_kh'];
		$ssFilter  = $arrParam['ssFilter'];
		if($options == null){
			$select = $db->select()
			->from('khachhang AS kh',array('id','name','status','email','phone','telephone','address'))
			->joinLeft('clb AS clb','kh.id_clb=clb.id',array('clb.id AS id_clb','clb.name AS tenclb'))
			->joinLeft('khachhang_cn AS khcn','khcn.id=kh.id',array("DATE_FORMAT(khcn.birthday,'%d/%m/%Y') AS birthday"))
			->where('kh.id =?',$id_kh);
				
			$result = $db->fetchRow($select);
	
		}
	
		return $result;
	}
	
	
	public function countItem($arrParam = null, $options = null){
		$ssFilter  = $arrParam['ssFilter'];
		if($options == null){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter, $config);
			$select = $db->select()
			->from('popup AS c', array('COUNT(c.id) AS totalItem'));
				
			$result = $db->fetchOne($select);
		}
		if($options['task'] == 'admin-list'){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter, $config);
			
			$select = $db->select()
			->from('popup AS c', array('COUNT(c.id) AS totalItem'));
			
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('c.name LIKE ?', $keywords, STRING);
			}

			$result = $db->fetchOne($select);
		}
		
		return $result;
	}
	
	public function itemInSelectbox($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		$ssFilter  = $arrParam['ssFilter'];
		if($options == null){
			$select = $db->select()
			->from('users AS cc',array('id','name','status'))
			->order('cc.id ASC');
			$result = $db->fetchPairs($select);
			
		}
		return $result;
	}
	
	
	
	public function itemInPopup($arrParam = null, $options = null){
	
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		 
		$id=$arrParam['id'];
		$ssFilter  = $arrParam['ssFilter'];
		if($options == null){
			$select = $db->select()
			->from('popup AS po',array('id','name','status','content','create','id_user','status','width','height'))
			->where('po.id =?',$id);
				
			$result = $db->fetchRow($select);
	
		} 
		return $result;
	}
	
	public function userInSelectbox($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		$ssFilter  = $arrParam['ssFilter'];
		if($options == null){
			$select = $db->select()
			->from('users AS cc',array('id','name','status'))
			->order('cc.id ASC');
			$result = $db->fetchPairs($select);
				
		}
		//var_dump($result);die;
		return $result;
	}
	
	public function listItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		$ssFilter  = $arrParam['ssFilter'];
		if($options['task'] == 'admin-list'){
			
			$paginator = $arrParam['paginator'];
			
					$select = $db->select()
							->from('popup AS po',array('*',"DATE_FORMAT(po.create,'%d/%m/%Y') AS create"))
							->joinLeft('users AS u','po.id_user=u.id',array('u.name AS name_user'));
							 
			if($paginator['itemCountPerPage'] > 0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page, $rowCount);
			}
			
			$result = $db->fetchAll($select);
		}
		
		return $result;
		
	}

	public function saveItem($arrParam = null, $options = null){
		//var_dump($arrParam);die;
		$filter = new Zend_Filter_Alnum(true);
		if($options['task'] == 'admin-add'){
			$row 				= $this->fetchNew();
			$info 					= new Zendvn_System_Info();
			$createby 			= $info->getMemberInfo('id');
			 
			$row->id_user 			= $createby;
			$row->name 				= stripslashes($arrParam['name']);
			$row->width 			= stripslashes($arrParam['width']);
			$row->height 			= stripslashes($arrParam['height']);
			$row->content			= stripslashes($arrParam['content']);
			$row->create 			= @date("Y-m-d h:i:s");
			$row->status		 	= '1';
			$row->count		 		= '0';
			$row->save();
		}
	
		if($options['task'] == 'admin-edit'){
				
			$where 				= ' id=' . $arrParam['id'];
			$row 				= $this->fetchRow($where);
		
			$info 					= new Zendvn_System_Info();
			$createby 			= $info->getMemberInfo('id');

			$row->id_user 			= $createby;
			$row->name 				= stripslashes($arrParam['name']);
			$row->width 			= stripslashes($arrParam['width']);
			$row->height 			= stripslashes($arrParam['height']);
			$row->content			= stripslashes($arrParam['content']);
			$row->create 			= @date("Y-m-d h:i:s");
			$row->status		 	= '1';
			
			$row->save();
		}
		
		if($options['task'] == 'public-hits'){
			$where 				= ' id=' . $arrParam['id'];
			$row 				= $this->fetchRow($where);
			
			$row->hits 		= $arrParam['hits'] + 1;
			
			$row->save();
		}
	}
	
	
	public function SaveKhachhang($arrParam = null, $options = null){
		 
		$id=$arrParam['id']; 
		$filter = new Zend_Filter_Alnum(true);
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		$table = 'khachhang';
		$bind = array(
				'name'				=>$arrParam['name'],
				'email'				=>$arrParam['email'],
				'phone'				=>$arrParam['phone'],
				'address'			=>$arrParam['address'],
				//'id_hoithao'		=>$arrParam['id_hoithao'],
				//'id_chiendich'		=>$arrParam['id_chiendich'],
				'created'			=>@date("Y-m-d h:i:s"),
				'id_nguon_dk'=>'2',
				'status_tiemnang'=>'1',
				
				'id_clb'=>$arrParam['id_clb'],
				 
		);
		 
		$db->insert($table, $bind);
		
		$idnew= $db->lastInsertId();
		$table1	='khachhang_cn';
		$bind1	=array(
					'id'			=>$idnew,
					'birthday'		=>$arrParam['birthday'],
					'yahoo'			=>$arrParam['yahoo'],
					'skype'			=>$arrParam['skype'],
				); 
		$db->insert($table1, $bind1);
		$sql ='UPDATE popup SET count = count + 1 WHERE id=' .$id;
		$db->query($sql);
		
	}

	
	
	public function deleteItem($arrParam = null, $options = null){
		if($options['task'] == 'admin-delete'){				
			$where = ' id=' . $arrParam['id'];
			echo $where;die;
			$result = $this->delete($where);
		}
	
		if($options['task'] == 'admin-delete-muti'){
			$cid = explode(',', $arrParam['cid']);
			if(!empty($cid) && isset($arrParam['cid'])){
				$ids = implode(',', $cid);
				$where = 'id IN (' . $ids . ')';
				echo $where;die;
				$this->delete($where);
			}
	
		}
	}
	
	public function changeStatus($arrParam = null, $options = null){
		$cid = $arrParam['cid'];
		if(count($cid) > 0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
	
			$id = implode(',', $cid);
			$data = array('status' => $status);
			$where = 'id IN (' . $id . ')';
			$this->update($data, $where);
		}
		if($arrParam['id'] > 0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
			$data = array('status' => $status);
			$where = 'id = ' . $arrParam['id'];
			$this->update($data, $where);
		}
	}
	
	public function sortItem($arrParam = null, $options = null){
		$cid = $arrParam['cid'];
		$order = $arrParam['order'];
		if($options['task'] == 'admin-sort'){
			if(count($cid) > 0){
				foreach ($cid as $key => $val){
					$data = array('order' => $order[$val]);
					$where = 'id = ' . $val;
					$this->update($data, $where);
				}
			}
		}
	}
}